package sortArrayByParity;

public class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int l = 0,r = nums.length-1;
        while (l<r) {
            // 交换偶数和奇数的位置
            if(nums[l]%2==0) {
                l++;
            }else if(nums[r]%2!=0) {
                r--;
            }else {
                swap(nums,l,r);
                l++;
                r--;
            }
        }
        return nums;
    }
    private void swap(int[] nums,int i,int j) {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
}
